﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL.Facades
{
    public class AnagraficaFamiglieFacade : FacadeBase<AnagraficaFamiglieFacade>
    {
        FCDContext m_ctx = null;

        public Repository<FCD_AnagraficaFamiglie, Guid> Repo { get; internal set; }
        public Repository<FCD_Fornitori, Guid> RepoFornitori { get; internal set; }


        public AnagraficaFamiglieFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new FCDContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<FCD_AnagraficaFamiglie, Guid>(m_ctx);
            RepoFornitori = new Repository<FCD_Fornitori, Guid>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
                RepoFornitori.Dispose();
            }
        }

        public ReturnValue<bool> Add(FCD_AnagraficaFamiglie item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.Id == Guid.Empty)
                {
                    item.Id = Guid.NewGuid();
                }

                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(FCD_AnagraficaFamiglie item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(FCD_AnagraficaFamiglie item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_AnagraficaFamiglie> Get(Guid id)
        {
            ReturnValue<FCD_AnagraficaFamiglie> retValue = new ReturnValue<FCD_AnagraficaFamiglie>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_AnagraficaFamiglie>> GetAll()
        {
            ReturnValue<IQueryable<FCD_AnagraficaFamiglie>> retValue = new ReturnValue<IQueryable<FCD_AnagraficaFamiglie>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IEnumerable<NameValue<string, Guid>>> GetAllNameValue()
        {
            ReturnValue<IEnumerable<NameValue<string, Guid>>> retValue = new ReturnValue<IEnumerable<NameValue<string,Guid>>>();
            retValue.Value = null;
            retValue.HasValue = false;

            var objs = from x in m_ctx.FCD_AnagraficaFamiglies 
                       select new NameValue<string, Guid>() {
                           value = x.Id,
                           name = x.Descrizione
                       };

            if (objs.Any())
            {
                retValue.HasValue = true;
                retValue.Value = objs;
            }
            else
            {
                NameValue<string, Guid> dummy = new NameValue<string, Guid>();
                dummy.value = Guid.Empty;
                dummy.name = "Nessun Elemento disponibile";

                List<NameValue<string, Guid>> m_l = new List<NameValue<string, Guid>>();
                m_l.Add(dummy);

                retValue.HasValue = true;
                retValue.Value = m_l.AsEnumerable();
            }

            return retValue;
        }
    }
}
